//
//  LFBHomeLabelViewCell.m
//  loveFreshBeen
//
//  Created by Feng on 2017/7/10.
//  Copyright © 2017年 feng. All rights reserved.
//

#import "LFBHomeLabelViewCell.h"
#import <UIImageView+WebCache.h>
#import <Masonry.h>
#import "LFBShoppingCartManager.h"
#import "LFBAnimationManager.h"

@interface LFBHomeLabelViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *productImageView;

@property (weak, nonatomic) IBOutlet UILabel *productName;
@property (weak, nonatomic) IBOutlet UILabel *productLabel;
@property (weak, nonatomic) IBOutlet UILabel *productDescLabel;
@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;
@property (weak, nonatomic) IBOutlet UILabel *partnerPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *marketPriceLabel;

@end

@implementation LFBHomeLabelViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setupNotifications];
    
    LFBClickProductView *clickProductView = [LFBClickProductView clickProductView];
    clickProductView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:clickProductView];
    
    [clickProductView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.specificsLabel).offset(0);
        make.trailing.offset(-2);
        make.height.offset(27);
        make.width.offset(96);
    }];
    
    [clickProductView addTarget:self action:@selector(clickProductViewAction:) forControlEvents:UIControlEventValueChanged];
    
    _clickProductView = clickProductView;
    
    //从购物车管理类中取初始数据,还是直接从对应模型中取?
    clickProductView.productCount = [[LFBShoppingCartManager sharedManager] productCountWithShoppingCartModelID:self.model.product_id];
}

- (void)setupNotifications
{
    //注册详情页面点击数量增减通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detailProductInfoViewNumChangeNotification:) name:LFBDetailProductInfoViewNumChangeNotification object:nil];
}

- (void)detailProductInfoViewNumChangeNotification:(NSNotification *)noti
{
    LFBShoppingCartModel *model = noti.userInfo[KDetailProductModel];
    if(self.model.product_id == model.product_id)
    {
        _clickProductView.productCount = model.buyNums;
    }
}

- (void)teardownNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)dealloc
{
    [self teardownNotifications];
}

- (void)clickProductViewAction:(LFBClickProductView *)clickProductView
{
//    _model.buyNums = clickProductView.count;
    
    if(clickProductView.isIncrease)
    {
        //增入单例购物车
        [[LFBShoppingCartManager sharedManager] addShoppingCartProductWithProdunctModel:_model andIsFirstTime:NO];
        
        //动画,参数: 动画起点CGPoint, 需要动画的view
        CGPoint imageViewCenter = self.productImageView.center;
        // 转换坐标
        CGPoint convertPoint = [self.productImageView convertPoint:imageViewCenter toView:kWindow];
        
        UIImageView *animationView = [[UIImageView alloc] initWithImage:self.productImageView.image];
        
        LFBAnimationManager *manager = [LFBAnimationManager new];
        [manager createAnimationWithStartPoint:convertPoint andView:animationView];
    }
    else
    {
        //将商品从购物车中减掉
        [[LFBShoppingCartManager sharedManager] subShoppingCartProductWithProdunctModel:_model];
    }
    
    //发送通知,更改购物车的badgeValue
    [[NSNotificationCenter defaultCenter] postNotificationName:LFBShoppingCartAnimationNotification object:self];
}

- (void)setModel:(LFBShoppingCartModel *)model
{
    _model = model;
    self.productName.text = model.name;
    self.specificsLabel.text = model.specifics;
    self.partnerPriceLabel.text = [NSString stringWithFormat:@"%@元",@(model.partner_price).description];
    self.marketPriceLabel.text = [NSString stringWithFormat:@"%@元",@(model.market_price).description];
    
    [self.productImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"v2_placeholder_square"]];
    
    
    self.productLabel.layer.cornerRadius = 5;
    self.productLabel.clipsToBounds = YES;
    self.productLabel.layer.borderColor = [UIColor redColor].CGColor;
    self.productLabel.layer.borderWidth = 1;
    
    self.productDescLabel.layer.cornerRadius = 5;
    self.productDescLabel.clipsToBounds = YES;
}

@end
